<?php

/*************************************************************************
List entries
----------------------------------------------------------------
AvaCMS Project
Company: Avasoft Co., Ltd                                  
Name: Tran Thi Kim Que
Last updated: 08/03/2011
**************************************************************************/
include_once(ROOT_PATH."classes/dao/upload.class.php");
include_once(ROOT_PATH.'classes/dao/static.class.php');
include_once(ADMIN_ROOT_PATH.'includes/functions.php');
include_once(ROOT_PATH."classes/data/textfilter.class.php");
$templateFile = 'manageaddstaticpage.temp.html';
$items_per_pages = DEFAULT_ADMIN_ROWS_PER_PAGE;
$gallery = ROOT_PATH."gallery/avatar_upload/";
$gallery_new = $gallery."static/";
$gallery_storage = $gallery_new."/storage/";
$gallery_avatar = $gallery_new."/avatar/";

function validateData($request) {
	global $amessages;
	$infoText = '';
	$validate = new Validate();
	$infoText .= $validate->validString($amessages['title'],$request->element('title'));
	$infoText .= $validate->validString($amessages['details'],$request->element('details'));				
	return $infoText;
}
$static = new StaticPage();
$infoText = '';
$infoClass = 'infoText';
# Read parameters from request
$lang = $request->element('addLang','-1');
$status = $request->element('addStatus',-1);
$title = $request->element('title','');
$sapo = $request->element('sapo','');
$details = $request->element('details','');
$position = $request->element('position',0);
$home = $request->element('frontend',0);
$page = $request->element('page',1);
$pid = $request->element('pid','');

# Generate status combobox for Search form
$listStatus = optionStatus($request->element('searchStatus','-1'));
$template->assign("listStatus",$listStatus);

# Generate status combobox for Search form
$listLang = optionLang($request->element('searchLang','-1'));
$template->assign("listLang",$listLang);
# Generate status combobox for Search form
$listPid = $static->optionAllCategories('id','title',$pid);
$template->assign("listPid",$listPid);
if($_POST) {
	include_once(ROOT_PATH."classes/data/validate.class.php");
	$files = isset($_FILES['avatar'])?$_FILES['avatar']:'';
	# Validation
	$validate = validateData($request);
	if($validate == '') {
		
		if($files!=''){ 
			$upload = new Upload($files);
			$copy = $upload->moveFile($gallery_storage);
			$file_name = $upload->getNameFile();
			if ($upload->isImage($file_name))
			$copy_avatar = $upload->resize($gallery_storage,$gallery_avatar, $file_name, $file_name,165,142,0);
		}else $file_name ='';
		$slug=removeSign($title);
		$data = new Staticinfo($slug,$lang,$title,'',$sapo,$details,$status,$position,0,$file_name,$pid);
		$fields = array ('slug'=>$data->getSlug(),
						'lang'=>$data->getLang(), 
						'title'=>$data->getTitle(),
						'keywords'=>$data->getKeywords(),
						'sapo'=>$data->getSapo(),
						'details'=>$data->getDetails(), 
						'status'=>$data->getStatus(), 
						'position'=>$data->getPosition(),
						'home'=>$data->getHome(),
						'block'=>$data->getBlock(),
						'properties'=>$data->getProperties()
						);
		$static->addData(&$fields);
		$infoText = $amessages['item_inserted_ok'];
		$infoClass = "InfoText";
		$act = 'list';
		$template->assign("act",$act);
		$templateFile = "manageliststaticpage.temp.html";
		$pages = $static->getNumItems('id');
		$rows = $pages['rows'];
		$pages = $pages['pages'];
		
		# Get all entries object and pass to template
		$staticItems = $static->getObjects($page, "1=1", array('position'=>'ASC'));
		$template->assign("listObjects",$staticItems);
		if($page > $pages) $page = $pages;		
		# Generate and pass link pager to template
		$listPage = linkPager(ADMIN_SCRIPT."?op=manage&amp;part=staticPage&amp;act=list&amp;page=%s",$pages,$page);
		$template->assign("adminPager",$listPage);
		$template->assign("infoText",$infoText);
		$template->assign("infoClass",$infoClass);
		$template->assign("rows",$rows);
		$template->assign("pages",$page);
		
	} else {
		$infoText = $amessages['item_inserted_error']."<br />".$validate;
		$infoClass = "errorText";	
		$template->assign("lang",$lang);
		$template->assign("status",$status);
		$template->assign("title",$title);
		$template->assign("sapo",$sapo);
		$template->assign("details",$details);
		$template->assign("position",$position);
		$template->assign("infoText",$infoText);
		$template->assign("infoClass",$infoClass);
		$template->assign("static",$static);
		$template->assign("pid",$pid);
		$template->assign("home",$home);
	}
}
?>